package com.ibt.dao;

import java.util.ArrayList;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ibt.entity.BugEntry;
import com.ibt.util.ApplicationContext;

public class BugEntryDAO {
	static Transaction tx;

	public static int insert(BugEntry b) throws Exception {

			Session session =  ApplicationContext.getHiberSession().openSession();
			Transaction tx=null;
			try {
			     tx = session.beginTransaction();
				session.saveOrUpdate(b);
				tx.commit();
				return 0;
			}
			catch (Exception ex) {
			     if (tx!=null) tx.rollback();
			     throw ex;
			}
			finally {
			     session.close();
			}
	}

	public static int update(BugEntry b) throws Exception {
		Session session = startOperations();
		Query qry = session
				.createQuery("update BugEntry set current_points=:points where business_id=:busid and customer_id=:custid");
		//qry.setInteger("points", b.getCurrentPoints());
		////qry.setInteger("busid", b.getBusiness().getBusinessId());
		//qry.setInteger("custid", b.getCustomer().getCustomerId());
		Object ob = qry.executeUpdate();
		session.getTransaction().commit();
		endOperations(session);
		return 0;
	}

	public static int delete(BugEntry b) throws Exception {
		Session session = startOperations();
		session.delete(b);
		// tx.commit();
		endOperations(session);
		return 0;
	}

	private static void endOperations(Session session) {
		session.close();
		session = null;
	}

	private static Session startOperations() throws Exception {
		Session session = ApplicationContext.getHiberSession().openSession();
		tx = session.beginTransaction();
		return session;
	}

	public static List<BugEntry> findAll(int businessId) throws Exception {
		List<BugEntry> custObjs = new ArrayList<BugEntry>();
		List objs = null;
		Session session = startOperations();
		Query query = session.createQuery("from BugEntry where business_id=:busid");
		query.setInteger("busid", businessId);
		objs = query.list();
		for (Object o : objs) {
			custObjs.add((BugEntry) o);
		}
		endOperations(session);
		return custObjs;
	}

	public static BugEntry findById(int i) throws Exception {
		Session session = startOperations();
		BugEntry c = (BugEntry) session.load(
				BugEntry.class, i);
		endOperations(session);
		return c;
	}

	public static Object getByCustBusId(int id, int i) throws Exception {
		Session session = startOperations();
		System.out.println("id in dao " + id);
		Query qry = session
				.createQuery("from BugEntry where customer_id=:id and business_id=:bid");
		qry.setInteger("id", id);
		qry.setInteger("bid", i);
		Object obj = qry.uniqueResult();
		System.out.println("obj is " + obj);

		endOperations(session);
		return obj;

	}

	public static List<BugEntry> getBusinessCustMap(int userid)
			throws Exception {
		Session session = startOperations();
		System.out.println("id in dao " + userid);
		Query qry = session
				.createQuery("from BugEntry where customer_id=:userid");
		qry.setInteger("userid", userid);
		List<Object> obj = qry.list();
		ArrayList<BugEntry> lst = new ArrayList<BugEntry>();
		for (Object o : obj) {
			System.out.println("obj is " + obj);
			lst.add((BugEntry) o);
		}
		endOperations(session);
		return lst;

	}

	public static List<BugEntry> findByBusId(int bsid) throws Exception {

		Session session = startOperations();
		Query qry = session
				.createQuery("from BugEntry where business_id=:id");
		qry.setInteger("id", bsid);
		List<BugEntry> listBC = qry.list();
		endOperations(session);
		return listBC;
	}
	public static BugEntry getByRefNo(int busid,int  refNo) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     String querystr="From BugEntry where business_id=:busid and id=:refNo";
		     Query query=session.createQuery(querystr);
		     query.setInteger("refNo", refNo);
		     query.setInteger("busid", busid);
		     Object queryresult=query.uniqueResult();
		     BugEntry comp=(BugEntry)queryresult;
		     return comp;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}


}
